/**
 * Tool to set a sequence of inputs.
 * 
 * @author Giuseppe Mendola (giuseppe.mendola@usi.ch); 
 */

package ch.usi.inf.pf2.gui;

import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;

import ch.usi.inf.pf2.Component;
import ch.usi.inf.pf2.Value;
import ch.usi.inf.pf2.pins.OutputPin;
import ch.usi.inf.pf2.puts.Input;


public class ArrayListInputTool extends Tool {

	public ArrayListInputTool(Model model) {
		super(model, "Multiple Input", "multiple.gif");
	}
	private Component selectedComponent; 
	
	public void mouseClicked(final MouseEvent ev) {
		final int x = ev.getX();
		final int y = ev.getY();
		selectedComponent = getDrawing().findComponentAt(x, y);
		System.out.println("mouseClicked on " + selectedComponent);
		if (!ev.isShiftDown()) {
			getSelection().clear();
		}
		if (selectedComponent != null) {
			getSelection().add(selectedComponent);
		}
		if(selectedComponent instanceof Input) {
			String input=JOptionPane.showInputDialog("set input");
			if (input.matches("[a-zA-Z2-9]+")){
				JOptionPane.showMessageDialog(null ,"not allowed! put in binary values");
				return;
			}
			final ArrayListInput multipleInput=new ArrayListInput(input, selectedComponent);
			getDrawing();
			Drawing.getInputs().clear();
			getDrawing();
			Drawing.getInputs().add((Input) selectedComponent);
			Model.getDrawing().setMultipleInput(true);
			((Input) selectedComponent).setValue(Value.UNKNOWN);
			((Input) selectedComponent).setOutputPin(new OutputPin(Value.UNKNOWN));
			((Input) selectedComponent).setInputString(input);
			((Input) selectedComponent).setWidth(input.length()*8+20);
			((Input) selectedComponent).setMultipleInput(true);
			Model.getDrawing().add(multipleInput);
			System.out.println("added   "+selectedComponent+ "  to inputs");
		}
	}

}

